//
//  CAGoodsListHeaderView.m
//  LNH
//
//  Created by twentylx on 2018/2/6.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAGoodsListHeaderView.h"

@implementation CAGoodsListHeaderView

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.searchView];
        [self.searchView addSubview:self.searchBar];
        [self addSubview:self.vipImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.lineView];
        
        [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            make.top.equalTo(self);
            make.height.mas_equalTo(44);
        }];
        
        [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.searchView).offset(30);
            make.top.equalTo(self.searchView).offset(10);
            make.right.equalTo(self.searchView).offset(-30);
            make.height.mas_equalTo(30);
        }];
        
        [_vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            make.top.equalTo(self.searchView.mas_bottom).offset(20);
            make.height.mas_equalTo(150);
        }];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            make.bottom.equalTo(self.lineView).offset(-20);
        }];
        
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(ONE_PIXEL);
        }];
    }
    return self;
}

#pragma mark get & set
- (UIView *)searchView {
    if (!_searchView) {
        _searchView = [UIView new];
        _searchView.backgroundColor = [UIColor whiteColor];
    }
    return _searchView;
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [UISearchBar new];
        _searchBar.barTintColor = [UIColor clearColor];
        _searchBar.backgroundColor = [UIColor clearColor];
        _searchBar.tintColor = [UIColor clearColor];
        _searchBar.placeholder = @" 搜 索";
        _searchBar.layer.cornerRadius = 10;
        _searchBar.backgroundImage = [self GetImageWithColor:RGBCOLOR(241, 241, 241) andHeight:30];
        [_searchBar setSearchFieldBackgroundImage:[self GetImageWithColor:RGBCOLOR(241, 241, 241) andHeight:30] forState:UIControlStateNormal];
        
        UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
        // 输入文本颜色
        searchField.textColor = RGBCOLOR(207, 89, 38);
        searchField.font = [UIFont systemFontOfSize:16];
        // 默认文本颜色
        [searchField setValue:RGBCOLOR(207, 89, 38) forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _searchBar;
}

- (UIImageView *)vipImageView {
    if (!_vipImageView) {
        _vipImageView = [UIImageView new];
        _vipImageView.image = LMImageNamed(@"upgrade");
        _vipImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _vipImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"推荐商品";
        _titleLabel.font = [UIFont systemFontOfSize:20];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = RGBCOLOR(189, 189, 189);
    }
    return _lineView;
}

/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}
@end
